import Vue from 'vue'
import Vuex from 'vuex'
import vuexPersistedstate from "vuex-persistedstate";
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    historyList: [], // 历史记录
    show: true, // 程序是否显示
    isOnline: false, // 机器人是否已登录
    configShow: false, // 配置是否显示
    trigger: "#", // 机器人触发指令
    groupId: "", // 绑定群聊
    master: "", // 管理QQ
    // 白名单指令
    whiteCommandList: [
      "list",
      "say",
      "tps"
    ],
    platform: "2",
    RconHost: "0.0.0.0"
  },
  plugins: [
    vuexPersistedstate({
      storage: window.localStorage
    })
  ],
  getters: {
    historyList: (state) => state.historyList,
    show: (state) => state.show,
    isOnline: (state) => state.isOnline,
    configShow: (state) => state.configShow,
    trigger: (state) => state.trigger,
    groupId: (state) => state.groupId,
    master: (state) => state.master,
    whiteCommandList: (state) => state.whiteCommandList,
    platform: (state) => state.platform,
    RconHost: state => state.RconHost,
  },
  mutations: {
    SET_HISTORYLIST(state, value) {
      state.historyList.splice(0, state.historyList.length, ...value)
    },
    SET_SHOW(state, value) {
      state.show = value
    },
    SET_ISONLINE(state, value) {
      state.isOnline = value
    },
    SET_CONFIGSHOW(state, value) {
      state.configShow = value
    },
    SET_TRIGGER(state, value) {
      state.trigger = value
    },
    SET_GROUPID(state, value) {
      state.groupId = value
    },
    SET_MASTER(state, value) {
      state.master = value
    },
    SET_WHITECOMMANDLIST(state, value) {
      state.whiteCommandList.splice(0, state.whiteCommandList.length, ...value)
    },
    SET_PLATFORM(state, value) {
      state.platform = value
    },
    SET_RCONHOST(state, value) {
      state.RconHost = value
    }
  },
  actions: {
    Set_History_List(store, val) {
      let list = JSON.parse(JSON.stringify(this.state.historyList));
      if (val === "clear") {
        store.commit("SET_HISTORYLIST", []);
        return false;
      }
      const index = list.findIndex(ev => ev.account === val.account);
      if (index === -1) {
        list.push(JSON.parse(JSON.stringify(val)))
        store.commit("SET_HISTORYLIST", list);
      } else {
        list[index] = JSON.parse(JSON.stringify(val));
        store.commit("SET_HISTORYLIST", list);
      }
    },
    Set_Show(store, val) {
      store.commit("SET_SHOW", val)
    },
    Set_IsOnline(store, val) {
      store.commit("SET_ISONLINE", val)
    },
    Set_ConfigShow(store, val) {
      store.commit("SET_CONFIGSHOW", val)
    },
    Set_Trigger(store, val) {
      store.commit("SET_TRIGGER", val)
    },
    Set_GroupId(store, val) {
      store.commit("SET_GROUPID", val)
    },
    Set_Master(store, val) {
      store.commit("SET_MASTER", val)
    },
    Set_WhiteCommandList(store, val) {
      store.commit("SET_WHITECOMMANDLIST", val)
    },
    Set_Platform(store, val) {
      store.commit("SET_PLATFORM", val)
    },
    Set_RconHost(store, val) {
      store.commit("SET_RCONHOST", val)
    }
  }
})